package cn.edu.seig.textbook.mapper;

import cn.edu.seig.textbook.domain.pojo.Requests;
import cn.edu.seig.textbook.enums.RequestEnum;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author day
 * @since 2024-11-07
 */
@Mapper
public interface RequestsMapper extends BaseMapper<Requests> {
    // 根据申请状态进行模糊查询（联合查询 tb_teachers 和 tb_requests）
    @Select("SELECT r.request_id, t.name AS teacher_name, r.request_content, r.request_status " +
            "FROM tb_requests r " +
            "JOIN tb_teachers t ON r.teacher_id = t.teacher_id " +
            "WHERE r.request_status = #{requestStatus}")
    List<Requests> getRequestsByStatus(@Param("requestStatus") RequestEnum requestStatus);

    // 根据教师名模糊查询（联合查询 tb_teachers 和 tb_requests）
    @Select("SELECT r.request_id, t.name AS teacher_name, r.teacher_id, r.request_content, r.request_status " +
            "FROM tb_requests r " +
            "JOIN tb_teachers t ON r.teacher_id = t.teacher_id " +
            "WHERE t.name LIKE CONCAT('%', #{teacherName}, '%')")
    List<Requests> getRequestsByTeacherName(@Param("teacherName") String teacherName);

    // 添加申请
    @Insert("INSERT INTO tb_requests (teacher_id, request_content, request_status) " +
            "VALUES (#{teacherId}, #{requestContent}, #{requestStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "requestId")
    int addRequest(Requests request);

    // 修改申请
    @Update("UPDATE tb_requests SET request_content = #{requestContent}, request_status = #{requestStatus} " +
            "WHERE request_id = #{requestId}")
    int updateRequest(Requests request);

    // 删除申请
    @Delete("DELETE FROM tb_requests WHERE request_id = #{requestId}")
    int deleteRequest(@Param("requestId") int requestId);
}
